package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

public class ViewPPMAttachedFiles extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int year = WebUtil.getParamValueAsInteger(request, "year", 0);
		int month = WebUtil.getParamValueAsInteger(request, "month", 0);
		int category = WebUtil.getParamValueAsInteger(request, "category", 0);

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		int i = 0;
		String jpql = "select h from HospitalDevicePpmDetail h where h.visitStatus != "
				+ Enums.PPM_VISIT_STATUS.NOT_HAPPENED.getId() + "  ";
		if (month != 0) {
			jpql += " and MONTH(h.visitDate) = ? ";
			map.put(++i, month);
		}

		if (month != 0) {
			jpql += " and YEAR(h.visitDate) = ? ";
			map.put(++i, year);
		}

		if (category != 0) {
			jpql += " and h.hospitalDevice.lookupDeviceCategory.id = ? ";
			map.put(++i, category);
		}
		jpql += " order by h.hospitalDevice.deviceName ";

		List<HospitalDevicePpmDetail> ppmList = dbm.queryJPQL(jpql, map, HospitalDevicePpmDetail.class);
		if (ppmList == null || ppmList.isEmpty()) {
			announceErorrMessage(request, response,
					"لم يتم إدخال معلومات الزيارات الوقائية للأجهزة المطلوبة في الزمن الذي تم تحديده");
		} else {
			request.setAttribute("ppmList", ppmList);
			viewHTMLReport(request, response, "report/ViewPPMAttachedFiles.jsp",
					Enums.REPORT_TITLE.PPM_ATTACHED_FILES.getReportTitle());
		}
	}

}
